import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/home/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/RequestDio.dart';

import '../../api/home.dart';
import 'home_notify_list.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _notifyList = [];
  int _count = 60;
  Timer? _timer;
  @override
  void initState() {
    super.initState();
    getData();
  }

  getData() async {
    // 前端请求服务器会有跨域问题
    // final res =
    //     await requestDio._dio.get('https://live-api.itheima.net/announcement');
    // print(res.data);
    final res = await getNotifyListAPI();
    setState(() {
      _notifyList = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        elevation: 0,
        title: Text(
          '享+社区',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.transparent,
      ),
      body: ListView(
        padding: EdgeInsets.all(10),
        children: [
          Row(
            children: [
              ElevatedButton(
                  onPressed: () {
                    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
                      _count--;
                      setState(() {});
                    });
                  },
                  child: Text('开始')),
              Text('还剩$_count秒'),
              ElevatedButton(
                  onPressed: () {
                    _timer?.cancel();
                  },
                  child: Text('停止'))
            ],
          ),
          HomeNav(),
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset(
              'assets/images/banner@2x.jpg',
              fit: BoxFit.cover,
            ),
          ),
          //公告区
          HomeNotifyList(
            notifyList: _notifyList,
          )
        ],
      ),
    );
  }
}
